package com.origin.baselibrary.activity.style

import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.google.android.material.appbar.AppBarLayout
import com.origin.baselibrary.R
import com.origin.baselibrary.databinding.LayoutBaseParallaxBinding
import com.origin.baselibrary.viewmodel.BaseViewModel
import com.origin.utils.statusBar.StatusBarUtil
import kotlin.math.abs

abstract class BaseParallaxPageActivity<VB : ViewDataBinding, VM : BaseViewModel>(var layout: Int) :
    ViewModelActivity<LayoutBaseParallaxBinding, VM>(
        R.layout.layout_base_parallax,
        isSafeDis = false
    ),

    AppBarLayout.BaseOnOffsetChangedListener<AppBarLayout> {
    lateinit var mContentViewBinding: VB
    override fun initContentView() {

        mContentViewBinding = DataBindingUtil.inflate<VB>(
            LayoutInflater.from(mContext),
            layout,
            null,
            false
        )
        contentViewBinding.layoutContent.addView(mContentViewBinding.root)
        loadContentLayout(mContentViewBinding)
        contentViewBinding.toolbar.title = getToolbarTitle()
        setSupportActionBar(contentViewBinding.toolbar);

        val layoutParams: ViewGroup.MarginLayoutParams =
            contentViewBinding.toolbar.layoutParams as ViewGroup.MarginLayoutParams

        layoutParams.topMargin = StatusBarUtil.getStatusBarHeight(this);
        contentViewBinding.toolbar.setLayoutParams(layoutParams);

        contentViewBinding.appBar.addOnOffsetChangedListener(this)
        contentViewBinding.toolbar.setNavigationIcon(R.drawable.ic_back_file_picker);
        contentViewBinding.toolbar.setNavigationOnClickListener {
            finish()
        }
        // app:contentScrim=
        contentViewBinding.cToolbar.setContentScrimColor(Color.BLUE)

        modifyContentScrim().apply {
            if (this == -1) {
                contentViewBinding.cToolbar.setContentScrimColor(Color.BLUE)
            } else {
                contentViewBinding.cToolbar.setContentScrimColor(this)
            }
        }

        contentViewBinding.imgTop.setImageDrawable(getTapImage())
    }

    override fun onDestroy() {
        contentViewBinding.appBar.removeOnOffsetChangedListener(this)
        super.onDestroy()
    }

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        if (appBarLayout == null) {
            return
        }
        if (verticalOffset == 0) {
            // 完全展开
            expanded()
        } else if (abs(verticalOffset) >= appBarLayout.totalScrollRange) {
            // 完全折叠
            collapsed()
        } else {
            // 部分折叠
            onStateChanged(appBarLayout, verticalOffset)
        }
    }

    abstract fun getToolbarTitle(): String

    // 展开状态
    fun expanded() {}

    // 折叠状态
    fun collapsed() {}

    // 中间状态
    fun onStateChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {}

    open fun modifyContentScrim(): Int {
        return -1
    }

    abstract fun getTapImage(): Drawable

    abstract fun loadContentLayout(mContentViewBinding: VB)

}